/**
 * Description:
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2022-08-24 10:09
 */

import com.sun.jna.ptr.IntByReference;

import java.io.FileOutputStream;


/**
 * a参数在说明文档中确定了最大的长度
 * b参数是C++的int类型长度为4个字节
 * 方法中a参数返回查询到的值，b参数返回a的长度
 * java中数组在创建时就需要确定好长度，所以b参数没有意义
 */
public class Card {

    /**
     * 得到姓名信息
     *
     * @return 姓名信息
     */
    public static String GetPeopleName() throws Exception {
        try {
            byte[] a = new byte[30];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetPeopleName(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到性别信息
     *
     * @return 性别信息
     */
    public static String GetPeopleSex() throws Exception {
        try {
            byte[] a = new byte[2];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetPeopleSex(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到民族信息
     *
     * @return 民族信息
     */
    public static String GetPeopleNation() throws Exception {
        try {
            byte[] a = new byte[20];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetPeopleNation(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到出生日期信息
     *
     * @return 到出生日期信息
     */
    public static String GetPeopleBirthday() throws Exception {
        try {
            byte[] a = new byte[16];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetPeopleBirthday(a, b) == 1 ? new String(a).trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到地址信息
     *
     * @return 地址信息
     */
    public static String GetPeopleAddress() throws Exception {
        try {
            byte[] a = new byte[70];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetPeopleAddress(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到卡号信息
     *
     * @return 卡号信息
     */
    public static String GetPeopleIDCode() throws Exception {
        try {
            byte[] a = new byte[36];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetPeopleIDCode(a, b) == 1 ? new String(a).trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到发证机关信息
     *
     * @return 发证机关信息
     */
    public static String GetDepartment() throws Exception {
        try {
            byte[] a = new byte[30];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetDepartment(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到有效开始日期
     *
     * @return 有效开始日期
     */
    public static String GetStartDate() throws Exception {
        try {
            byte[] a = new byte[16];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetStartDate(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到有效截止日期
     *
     * @return 有效截止日期
     */
    public static String GetEndDate() throws Exception {
        try {
            byte[] a = new byte[16];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.GetEndDate(a, b) == 1 ? new String(a, "gb2312").trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 得到安全模块号码
     *
     * @return 安全模块号码
     */
    public static String CVR_GetSAMID() throws Exception {
        try {
            byte[] a = new byte[4 * 10];
            byte[] b = new byte[4];
            return CardLibrary.INSTANCE.CVR_GetSAMID(a, b) == 1 ? new String(a).trim() : "";
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "";
    }

    /**
     * 获取照片，将图片写到指定路径
     *
     * @return 获取成功 boolean
     */
    public static boolean GetBMPData() throws Exception {
        try {
            byte[] a = new byte[3886200];
            IntByReference len = new IntByReference();
            FileOutputStream fos = new FileOutputStream("D:\\123.bmp");
            if (CardLibrary.INSTANCE.GetBMPData(a, len) == 1) {
                fos.write(a, 0, len.getValue());
                fos.flush();
                fos.close();
                return true;
            } else return false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * 获取照片，将图片写到指定路径
     *
     * @return 获取成功 boolean
     */
    public static boolean GetJpgData() throws Exception {
        try {    // 返回固定的38862字节，使用看图软件另存后5000字节左右，
            // 说明文档中没有明确说明返回的字节数。
            byte[] a = new byte[3886200];
            IntByReference len = new IntByReference();
            FileOutputStream fos = new FileOutputStream("D:\\123.jpg");
            if (CardLibrary.INSTANCE.GetJpgData(a, len) == 1) {
                System.out.println("len = " + len);
                fos.write(a, 0, len.getValue());
                fos.flush();
                fos.close();
                return true;
            } else return false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static boolean Getbase64BMPData() {
        try {    // 返回固定的38862字节，使用看图软件另存后5000字节左右，
            // 说明文档中没有明确说明返回的字节数。
            byte[] a = new byte[3886200];
            IntByReference len = new IntByReference();
            FileOutputStream fos = new FileOutputStream("D:\\123.txt");
            if (CardLibrary.INSTANCE.Getbase64BMPData(a, len) == 1) {
                System.out.println("len = " + len);
                fos.write(a, 0, len.getValue());
                fos.flush();
                fos.close();
                return true;
            } else return false;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }
}


